const http = require("http");

const { selectQK, insertOne, selectOne, shoppingAll, xiangqingAll, sqlinit, selectShopping, ShoppingList, selectremove, selectChangeNum } = require("./sqlData");
var connect;
http.createServer(async (req, res) => {
    var arr = req.url.split("?")[0];
    res.writeHead(200, {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json;charset=utf-8"
    })
    if (!connect) connect = await sqlinit();
    if (!connect) {
        res.end("数据库丢失!!!");
        return;
    }
    if (arr == "/zhuce") {
        var data = await getData(req);
        data = JSON.parse(data)
        var bool = await insertOne(connect, data)
        if (bool) {
            res.end(JSON.stringify("注册成功"))
        } else {
            res.end(JSON.stringify("注册失败"))
        }


    } else if (arr == "/dl") {
        var data = await getData(req);
        data = JSON.parse(data)
        var bool = await selectOne(connect, data)
        if (bool) {
            res.end(JSON.stringify("登录成功"))
        } else {
            res.end(JSON.stringify("登录失败"))
        }


    } else if (arr == "/list") {
        var data = await getData(req);
        var list = await shoppingAll(connect);
        res.end(JSON.stringify(list));


    } else if (arr == "/xiangqing") {
        var data = await getData(req);
        var list = await xiangqingAll(connect);
        res.end(JSON.stringify(list));


    } else if (arr == "/shopping") {
        var data = await getData(req);
        data = JSON.parse(data);
        var bools = await selectShopping(connect, data)

        if (bools) {
            res.end(JSON.stringify("商品添加成功"))
        } else {
            res.end(JSON.stringify("商品添加失败"))
        }


    } else if (arr == "/shoppinglist") {
        var data = await getData(req);
        var shoppinglist = await ShoppingList(connect)
        res.end(JSON.stringify(shoppinglist))


    } else if (arr == "/remove") {
        var data = await getData(req);
        data = JSON.parse(data);
        var bools = await selectremove(connect, data)

        if (bools) {
            res.end(JSON.stringify({ msg: "商品删除成功" }))
        } else {
            res.end(JSON.stringify({ msg: "商品删除失败" }))
        }


    } else if (arr == "/xiugai") {
        var data = await getData(req);
        data = JSON.parse(data);
        var bools = await selectChangeNum(connect, data)

        if (bools) {
            res.end(JSON.stringify({ msg: "商品数量修改成功" }))
        } else {
            res.end(JSON.stringify({ msg: "商品数量修改失败" }))
        }

        
    }else if(arr=="/qingkon"){
        var data = await getData(req);
        var bools = await selectQK(connect)

        if (bools) {
            res.end(JSON.stringify({ msg: "已清空" }))
        } else {
            res.end(JSON.stringify({ msg: "清空失败" }))
        }
    }
    else{
        var data=await getData(req);
        data=JSON.parse(data)
    }

}).listen(3860)

function getData(req) {
    return new Promise(function (resolve, reject) {
        var data = "";
        req.on("data", function (_data) {
            data += _data;
        });
        req.on("end", function () {
            resolve(data);
        })
    })
}
